using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using AVR109Boot;

namespace AVR109BootloaderGUI
{
    public partial class frmAVR109Bootloader : Form
    {
        bool verify = false;
        AVR109Bootloader avb;
        byte[] program;
        bool connected = false;
        public frmAVR109Bootloader()
        {

            InitializeComponent();

            avb = new AVR109Bootloader(this, AVRChipData.ATmega32, "COM1", 19200);

            avb.DataReceived += new AVR109Bootloader.DataReceivedEventHandler(avb_DataReceived);
            avb.Timeout += new AVR109Bootloader.TimeoutEventHandler(avb_Timeout);
            this.Closing += new CancelEventHandler(frmAVR109Bootloader_Closing);

            
        }

        void frmAVR109Bootloader_Closing(object sender, CancelEventArgs e)
        {
            avb.Dispose();
        }

        void avb_Timeout(object sender, EventArgs e)
        {
            MessageBox.Show("timeout");
        }

        void avb_DataReceived(object sender, AVR109Bootloader.DataReceivedEventArgs e)
        {
            byte[] bytes = avb.ReturnedData;
            if (verify)
            {
                int j = 0;
                foreach (byte b in avb.ReturnedData)
                {
                    if (b != program[j])
                    {
                        MessageBox.Show("Error at " + j);
                        break;
                    }
                    j++;
                }
                MessageBox.Show("verified");
                verify = false;
            }
            string s = "0x000000 ";
            int i = 1;
            foreach (byte b in bytes)
            {
                
                s += string.Format("{0:x2} ", b);
                if (i % 16 == 0)
                {
                    s += "\r\n0x"+ string.Format("{0:x6}",i)+" ";
                }
                i++;
            }
            textBox1.Text = s;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!connected)
            {
               // connected = avb.Connect();
            }

            label1.Text = avb.Task;
            progressBar1.Value = Convert.ToInt32(avb.PercentageCompletion);
            textBox2.Text = avb.Errors;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (program != null)
            {
                avb.WriteProgramMemoryBlockMode(program, true);
                verify = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            avb.ReadProgramMemory(0x4000, 0x0000);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Hex file|*.hex|All|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                IntelHexDecoder inthex = new IntelHexDecoder(0x83FF);
                string HexString = "0x000000 ";
                int i = 1;
                program = inthex.Decode(File.ReadAllText(ofd.FileName));
                foreach (byte b in program)
                {
                    HexString += string.Format("{0:x2} ", b);
                    if (i % 16 == 0)
                    {
                        HexString += "\r\n0x" + string.Format("{0:x6}", i) + " ";
                    }
                    i++;
                }
                textBox1.Text = HexString;
            }
        }
    }
}